package com.wbpay.sdk.dlg;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.wbpay.sdk.utils.Utils;

/**
 * Created by ck01- on 2017/9/19.
 */

public class CancelPayDialog extends Dialog implements View.OnClickListener {
    private DialogInterface.OnClickListener mOnOkClickListener;
    private DialogInterface.OnClickListener mOnCancelClickListener;

    public CancelPayDialog(Context context) {
        super(context);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    public void setOnOkClickListener(DialogInterface.OnClickListener l) {
        mOnOkClickListener = l;
    }

    public void setOnCancelClickListener(DialogInterface.OnClickListener l) {
        mOnCancelClickListener = l;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(Utils.getLayoutId(getContext(), "jy_dialog_cancelpay"));
        View exitOk = findViewById(Utils.getId(getContext(), "cancelpay_ok"));
        View exitCancel = findViewById(Utils.getId(getContext(), "cancelpay_cancel"));
        exitOk.setOnClickListener(this);
        exitCancel.setOnClickListener(this);
        updateWindow();
    }

    private void updateWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams a = window.getAttributes();
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        a.width = (int) (dm.widthPixels * 0.85f);
        window.setAttributes(a);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == Utils.getId(getContext(), "cancelpay_ok")) {
            dismiss();
            if (mOnOkClickListener != null) {
                mOnOkClickListener.onClick(this, 0);
            }
        } else if (v.getId() == Utils.getId(getContext(), "cancelpay_cancel")) {
            dismiss();
            if (mOnCancelClickListener != null) {
                mOnCancelClickListener.onClick(this, 1);
            }
        }
    }
}
